<?php

include_once 'metier/activite.dao.mysql.php';
include_once 'metier/activite.class.php';
include_once 'metier/gridlayout.class.php';
include_once 'metier/gridlayout.dao.mysql.php';
include_once 'metier/deplacelayout.class.php';
include_once 'metier/deplacelayout.dao.mysql.php';
include_once 'metier/speciallayout.class.php';
include_once 'metier/speciallayout.dao.mysql.php';


$db = new myPDO();

// Récuperer les données saisies par l'utilisateur
$tmpNameActi = $_POST["nom_acti_input"];
$tmpAdulte = $_POST["id_adulte"];
$pre_acti = new Activite(0, $tmpNameActi);
$activiteDAO = new ActiviteDAO();
$test = $activiteDAO->findActivite($pre_acti, $db);
if ($test == null) {

    /*     * ************************GRID LAYOUT ******************************* */
    if ($_POST['typeA'] == 'memory') {

        $tmpNbLignes = $_POST["nbLignes"];
        $tmpNbColonnes = $_POST["nbColonnes"];
        $tmpInstruct = $_POST["consigne1"];
	$tmpType = $_POST["typem"];
        $public = $_POST['public'];
        $nb = intval($tmpNbLignes) * intval($tmpNbColonnes);
        if((intval($nb)%2) != 0 ){
            echo '<script language="Javascript">
            alert ("Erreur, le nombre de case est impaire ! " );
             window.location = "../partie_generateur/ens_gestion_acti_ajout.html";
          </script>';
        }
        else {
            $tmpBordureCode = null;

            if ($_POST['typeBordure'] == "zeroB") {
                $tmpBordureCode = 0;
            }
            if ($_POST['typeBordure'] == "deuxB") {
                $tmpBordureCode = 2;
            }
            if ($_POST['typeBordure'] == "quatreB") {
                $tmpBordureCode = 4;
            }

            $extension = substr($_FILES["path_son_acti"]["name"], strrpos($_FILES["path_son_acti"]["name"], '.') + 1);
            $tmpNomSound = $_POST['nom_acti_input'] . '.' . $extension;

            $activite = new Activite(0, $tmpNameActi, $tmpNomSound, $tmpInstruct, $tmpBordureCode, $public, $tmpType, $tmpAdulte);

            $TMPactiviteID = $activiteDAO->createActivite($activite, $db);

            $activiteID = intval($TMPactiviteID);
            $tmpNomSound = $activiteID . '.' . $extension;
            $activite2 = new Activite($activiteID, $tmpNameActi, $tmpNomSound, $tmpInstruct, $tmpBordureCode, $public, $tmpType, $tmpAdulte);
            $activiteDAO->updateSoundActiviteSound($activite2, $db);
            
            $uploads_dir = "../sonsActivite/" . $tmpNomSound;

                
            move_uploaded_file($_FILES['path_son_acti']['tmp_name'], utf8_decode($uploads_dir));

            $gridLayout = new GridLayout(0, $tmpNbLignes, $tmpNbColonnes, $activiteID);
            $gridLayoutDAO = new GridLayoutDAO();
            $gridLayoutDAO->createGridLayout($gridLayout, $db);

            header('Location: ../partie_generateur/ens_gestion_acti_creation_memoire.html?id='.$activiteID.'&bd='.$tmpBordureCode.'&x='.$tmpNbLignes.'&y='.$tmpNbColonnes);
        }
    }

    /*     * ****************************** DEPLACE LAYOUT ************************************ */ 
    else if ($_POST['typeA'] == 'reeorder') {

        $tmpInstruct = $_POST["consigne2"];
        $tmpNbPicto = $_POST["nbPicto"];
        $tmpType = $_POST["type"];
        
        $public = $_POST['public'];
        

        if ($_POST['typeBordure2'] == "zero2B") {
            $tmpBordureCode = 0;
        }
        if ($_POST['typeBordure2'] == "deux2B") {
            $tmpBordureCode = 2;
        }
        if ($_POST['typeBordure2'] == "quatre2B") {
            $tmpBordureCode = 4;
        }

        $extension = substr($_FILES["path_son_acti2"]["name"], strrpos($_FILES["path_son_acti2"]["name"], '.') + 1);
        $tmpNomSound = utf8_decode($_POST['nom_acti_input']) . '.' . $extension;

        
        $activite = new Activite(0, $tmpNameActi, $tmpNomSound, $tmpInstruct, $tmpBordureCode, $public, $tmpType, $tmpAdulte);

        $TMPactiviteID = $activiteDAO->createActivite($activite, $db);
        $activiteID = intval($TMPactiviteID);
       $tmpNomSound = $activiteID . '.' . $extension;
            $activite2 = new Activite($activiteID, $tmpNameActi, $tmpNomSound, $tmpInstruct, $tmpBordureCode, $public, $tmpType, $tmpAdulte);
            $activiteDAO->updateSoundActiviteSound($activite2, $db);
            
            $uploads_dir = "../sonsActivite/" . $tmpNomSound;
        move_uploaded_file($_FILES['path_son_acti2']['tmp_name'], utf8_decode($uploads_dir));
        

        $dLayout = new DeplaceLayout(0, $tmpNbPicto, $activiteID);
        $dLayoutDAO = new DeplaceLayoutDAO();
        $dLayoutDAO->createDeplaceLayout($dLayout, $db);
        
        
        header('Location: ../partie_generateur/ens_gestion_acti_creation_reorder.html?id='.$activiteID.'&bd='.$tmpBordureCode.'&nb='.$tmpNbPicto);
       
    }
    /* SPECIAL LAYOUT */ 
    /*else {
        
        $tmpInstruct = $_POST["consigne3"];
             
        if ($_POST['typeBordure3'] == "zeroB") {
            $tmpBordureCode = 0;
        }
        if ($_POST['typeBordure3'] == "deuxB") {
            $tmpBordureCode = 2;
        }
        if ($_POST['typeBordure3'] == "quatreB") {
            $tmpBordureCode = 4;
        }
        
        echo $tmpBordureCode;
        
        //Image
        $extensionImg = substr($_FILES["img_acti"]["name"], strrpos($_FILES["img_acti"]["name"], '.') + 1);
        $tmpNomImgPrincipale = $_POST['nom_acti_input'].'-sp'.'.' . $extensionImg;
        $uploads_dir_img = "../images/".$tmpNomImgPrincipale;
        move_uploaded_file($_FILES['img_acti']['tmp_name'], $uploads_dir_img);
        
        //Son
        $extensionSound = substr($_FILES["path_son_acti3"]["name"], strrpos($_FILES["path_son_acti3"]["name"], '.') + 1);
        $tmpNomSound = $_POST['nom_acti_input'].'.'.$extensionSound;
        $uploads_dir_son = "../sonsActivite/".$tmpNomSound;
        move_uploaded_file($_FILES['path_son_acti3']['tmp_name'], $uploads_dir_son);
        
        //création activité
        $activite = new Activite(0, $tmpNameActi, $tmpNomSound, $tmpInstruct, $tmpBordureCode);
        $TMPactiviteID = $activiteDAO->createActivite($activite, $db);
        $activiteID = intval($TMPactiviteID);
        echo $activiteID;
        
        $spLayout = new SpecialLayout(0, $tmpNomImgPrincipale, $activiteID);
        $spLayoutDAO = new SpecialLayoutDAO();
        $spLayoutDAO->createSpecialLayout($spLayout, $db);
    }
*/
 
    
    //header('Location: ../partie_generateur/ens_gestion_acti_ajout_2.html');
} else {
    echo '<script language="Javascript">
            alert ("Erreur, cette activite existe deja ! " )
            window.location = "../partie_generateur/ens_gestion_acti_ajout.html";
          </script>';

}
?>